﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RbacPermissionManage.Application;
using RbacPermissionManage.Application.Admins;
using RbacPermissionManage.Application.Roles;
using RbacPermissionManage.Application.Roles.Dto;

namespace RbacPermissionManage.WebAPI.Controllers
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IRoleServices roleServices;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleServices"></param>
        public RoleController(IRoleServices roleServices)
        {
            this.roleServices = roleServices;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="createDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> CreateAsync(RoleCreateDto createDto)
        {
            return Ok(await roleServices.CreateAsync(createDto));
        }


        /// <summary>
        /// 显示查询分页角色列表
        /// </summary>
        /// <param name="searchDto"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetRoleAsync([FromQuery]SearchDto searchDto, int pageIndex, int pageSize)
        {
            var list = await roleServices.GetRoleAsync(searchDto, pageIndex, pageSize);
            return Ok(new
            {
                Code = Code.Success,
                Data = new
                {
                    PageData = list.Data.Item2,
                    Count = list.Data.Item1
                }
            });
        }

        /// <summary>
        /// 根据Id获取roleDto
        /// </summary>
        /// <param name="RoleId">角色Id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> BackfillAsync(int RoleId)
        {
            return Ok(await roleServices.BackfillAsync(RoleId));
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="updateDto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UpdateAsync(RoleUpdateDto updateDto)
        {
            return Ok(await roleServices.UpdateAsync(updateDto));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> DeleteAsync(int RoleId)
        {
            return Ok(await roleServices.DeleteAsync(RoleId));
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> BatchDelAsync(List<int> roleIds)
        {
            return Ok(await roleServices.BatchDelAsync(roleIds));
        }
    }
}
